package com.yc;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

//代码生成器
public class MpGenerator {


	public static void main(String[] args) {

		AutoGenerator mpg = new AutoGenerator();
		// 选择 freemarker 引擎，默认 Veloctiy
		mpg.setTemplateEngine(new FreemarkerTemplateEngine());
		// 全局配置
		final GlobalConfig gc = new GlobalConfig();
		gc.setAuthor("1850140986@qq.com");
		gc.setOutputDir("E:\\usedbook\\a013sssmpg\\src\\main\\java");
		gc.setFileOverride(true);// 是否覆盖同名文件，默认是false
		gc.setActiveRecord(false);// 不需要ActiveRecord特性的请改为false
		gc.setEnableCache(false);// 二级缓存
//		gc.setBaseResultMap(false);// XML ResultMap
//		gc.setBaseColumnList(false);// XML columList
		gc.setOpen(false);
		// gc.setSwagger2(true);//开启Swagger2
		gc.setDateType(DateType.ONLY_DATE);

		mpg.setGlobalConfig(gc);

		// 数据源配置
		DataSourceConfig dsc = new DataSourceConfig();
		dsc.setDbType(DbType.MYSQL);
		dsc.setTypeConvert(new MySqlTypeConvert() {
			@Override
			public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
				// 将数据库中decimal转换成BigDecimal
				if (fieldType.toLowerCase().contains("decimal")) {
					return DbColumnType.BIG_DECIMAL;
				}

				// 将数据库中datetime转换成date
				if (fieldType.toLowerCase().contains("datetime")) {
					return DbColumnType.DATE;
				}
				return super.processTypeConvert(gc, fieldType);
			}
		});
		dsc.setDriverName("com.mysql.cj.jdbc.Driver");
		dsc.setUsername("root");
		dsc.setPassword("root");
		dsc.setUrl("jdbc:mysql://localhost:3306/used_books");
		mpg.setDataSource(dsc);

		// 策略配置
		StrategyConfig strategy = new StrategyConfig();

		strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
		strategy.setTablePrefix(new String[] { "t_" });// 此处可以修改为您的表前缀

		// *******************************************************************************************
		//String[] tables = {"t_book_order","t_store","t_color","enterprise_directory","project_development"，"t_user_role","t_book_type","t_role","t_role_permission","t_permission","t_book_tv","t_book_comment","t_book_cart_like" };
		String[] tables = { ""};
		// *******************************************************************************************

		strategy.setInclude(tables); // 需要生成的表

		// 自定义实体父类
		strategy.setSuperEntityClass("com.yc.framework.baseClass.BaseEntity");
		// 自定义实体，公共字段
		// strategy.setSuperEntityColumns(new String[] { "id", "deleted", "name",
		// "create_time" });
		strategy.setSuperEntityColumns(new String[] { "id", "deleted", "create_time" });
		// 自定义 mapper 父类
		// strategy.setSuperMapperClass("com.baomidou.mybatisplus.core.mapper.BaseMapper");

		// 自定义 service 父类
		strategy.setSuperServiceClass("com.yc.framework.baseClass.BaseService");
		// 自定义 service 实现类父类
		strategy.setSuperServiceImplClass("com.yc.framework.baseClass.BaseServiceImpl");
		// 自定义 controller 父类
		strategy.setSuperControllerClass("com.yc.framework.baseClass.BaseController");
		// 使用restController
		strategy.setRestControllerStyle(true);

		// 使用lombok
		strategy.setEntityLombokModel(true);

		// 使用字段注解
		strategy.setEntityTableFieldAnnotationEnable(true);

		mpg.setStrategy(strategy);

		// 包配置
		PackageConfig pc = new PackageConfig();
		pc.setParent("com.yc.system");

		mpg.setPackageInfo(pc);
		mpg.setTemplate(new TemplateConfig().setXml(null));

		// 执行生成
		mpg.execute();
	}

}